﻿#region Copyright ©2008-2011, Technische Universitaet Muenchen
// ====================================================================================================
//
// Last Changed by       $Author: then $
// Last Changed Date     $LastChangedDate: 2009-05-16 14:58:56 +0200 (Sat, 16 May 2009) $
// Last Changed Revision $Rev: 144 $
//
// ====================================================================================================
#endregion

using System.Xml;
using Tum.CollabXT.TFS.Resources;

namespace Tum.CollabXT.TFS
{
    internal class GroupsAndPermissions : IProcessor
    {
        public string TaskFileFolder
        {
            get;
            set;
        }

        public string TaskFileName
        {
            get;
            set;
        }


        public bool Process(TemplateProcessor processor)
        {
            //Write log message
            processor.ToolProvider.ProcessLog.AddEntry(Language.Processing_GroupsPermissions);

            //Get default group name from template configuration
            var defaultGroupNode = processor.TemplateConfigDocument.SelectSingleNode("//Tasks.Groups/DefaultGroup");
            if (defaultGroupNode == null)
            {
                processor.ToolProvider.ProcessLog.AddEntry(string.Format(Language.Error_NotFound_InvalidTemplate, "Template config: //Tasks.Groups/DefaultGroup"), LogEntryType.Error);
                return false;
            }
            string defaultGroupName = defaultGroupNode.InnerText;


            string gapTemplateFileName = processor.ToolProvider.OutputPath + "/" + TaskFileFolder + "/" + TaskFileName;

            #region Load template
            XmlDocument gapDoc = new XmlDocument();
            processor.ToolProvider.ProcessLog.AddEntry(Language.Groups_Loading);
            try
            {
                gapDoc.Load(gapTemplateFileName);
            }
            catch
            {
                processor.ToolProvider.ProcessLog.AddEntry(string.Format(Language.Error_LoadingFailed, gapTemplateFileName), LogEntryType.Error);
                return false;
            }
            #endregion

            processor.ToolProvider.ProcessLog.AddEntry(Language.Groups_SelectingNode);
            var groupsNode = gapDoc.SelectSingleNode("/tasks/task[@id=\"GroupCreation\"]/taskXml/groups");
            if (groupsNode == null)
            {
                processor.ToolProvider.ProcessLog.AddEntry(string.Format(Language.Error_NotFound_InvalidTemplate, gapTemplateFileName + ": /tasks/task[@id=\"GroupCreation\"]/taskXml/groups"), LogEntryType.Error);
                return false;
            }

            //Get node which presents the default permission
            XmlNode defaultPermissionsNode = groupsNode.SelectSingleNode("group[@name='" + defaultGroupName + "']");
            if (defaultPermissionsNode == null)
            {
                processor.ToolProvider.ProcessLog.AddEntry(string.Format(Language.Error_NotFound_InvalidTemplate, gapTemplateFileName + ": /tasks/task[@id=\"GroupCreation\"]/taskXml/groups/group[@name='" + defaultGroupName + "']"), LogEntryType.Error);
                return false;
            }

            //Write
            foreach (IRole curRole in processor.ProcessProvider.Roles)
            {
                processor.ToolProvider.ProcessLog.AddEntry(string.Format(Language.Groups_WritingRole, curRole.Name));

                XmlNode newRoleNode = defaultPermissionsNode.CloneNode(true);
                newRoleNode.Attributes["name"].Value = curRole.Name;
                newRoleNode.Attributes["description"].Value = Helper.PrepareStringForTFS(curRole.Description, Helper.TFS_MaxWorkItemTextLen);

                groupsNode.AppendChild(newRoleNode);
            }

            #region Save groups and permissions file
            processor.ToolProvider.ProcessLog.AddEntry(Language.Groups_Saving);
            try
            {
                gapDoc.Save(gapTemplateFileName);
            }
            catch
            {
                processor.ToolProvider.ProcessLog.AddEntry(string.Format(Language.Error_SavingFailed, gapTemplateFileName), LogEntryType.Error);
                return false;
            }
            #endregion

            return true;
        }
    }
}